package com.sfh.appversion.base.page;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分页返回对象
 *
 * <p> {@link com.sfh.appveriosn.base.page.PageRequest}、{@link PageResponse}
 * 可以理解是防腐层的一种实现，不论底层 ORM 框架，对外分页参数属性不变
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页
     */
    private Long pageNow;

    /**
     * 每页显示条数
     */
    private Long pageSize = 10L;

    /**
     * 总数
     */
    private Long pageTotal;

    /**
     * 查询数据列表
     */
    private List<T> pageList = Collections.emptyList();

    public PageResponse(long pageNow, long pageSize) {
        this(pageNow, pageSize, 0);
    }

    public PageResponse(long pageNow, long pageSize, long pageTotal) {
        if (pageNow > 1) {
            this.pageNow = pageNow;
        }
        this.pageSize = pageSize;
        this.pageTotal = pageTotal;
    }

    public PageResponse(Page<T> page) {
        if (page != null) {
            this.pageSize = page.getSize();
            this.pageNow = page.getCurrent();
            this.pageTotal = page.getTotal();
            this.pageList = page.getRecords();
        }else {
            this.pageNow = 1L;
            this.pageSize = 10L;;
            this.pageTotal = 0L;
        }
    }

}